#include <stdio.h>
#include <conio.h>
#include <math.h>
#include <string.h>
long Hex_to_Dec(char strH[])//ham chuyen so he 16(Hexa) thanh so he 10(Decima) 
{
	int i,l,sm,kq=0,gt[50];
	l=strlen(strH);
	
	for(i=0; i<l; i++)
	{	
		sm=l-1-i;
		switch(strH[i])
		{
			case '0': gt[i]=0; break;
			case '1': gt[i]=1; break;
			case '2': gt[i]=2; break;
			case '3': gt[i]=3; break;
			case '4': gt[i]=4; break;
			case '5': gt[i]=5; break;
			case '6': gt[i]=6; break;
			case '7': gt[i]=7; break;
			case '8': gt[i]=8; break;
			case '9': gt[i]=9; break;
			case 'A': gt[i]=10; break;
			case 'B': gt[i]=11; break;
			case 'C': gt[i]=12; break;
			case 'D': gt[i]=13; break;
			case 'E': gt[i]=14; break;
			case 'F': gt[i]=15; break;
			default: printf("khong hop le");
		}
			kq=kq+(gt[i]*pow(16,sm));	
	} 
	return kq;
}
long n_to_Dec(long n, int coso)//ham chuyen so he co so bat ki(<10) thanh so he 10(Decima)
{
	int gt=0,dem=0;
	do
	{
		gt=gt+(n%10)*pow(coso,dem);
		n=n/10;
		dem++;
	}
	while(n!=0);
	return gt;
}
void dec_to_n(long n, int coso)//ham chuyen so he 10(Decima) thanh so he co so bat ki(<=16)
{
	char cs[]={'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
	char gt[100];
	int i=0;
	while(n>0)
	{
		gt[i++]=cs[n%coso];
		n=n/coso;
	}
	i--;
	for(; i>=0;i--)
	printf("%c", gt[i]);
}
int test(long n, int cs)//ham kiem tra tinh dung dan cua cac so nhap vao tu ban phim
{
	int dem=0;
	do
		{
		if(n%10>=cs)
		dem++;
		n=n/10;
		}
		while(n!=0);
	return dem;	
}
main()
{
	char strH1[50],strH2[50]; long n,m,x,y,z,t; int coso_o,coso_c;
	printf("_____CHUONG TRINH THUC HIEN TINH TOAN VOI SO TRONG HE DEM_____");
	printf("\n\n Giao vien huong dan: LE CAM TU");
	nhapcoso:
	{
	printf("\n\n\nNhap co so goc: ");
	scanf("%d", &coso_o);
	}
	if(coso_o<=1||coso_o>16)
	{
	printf(" Nhap sai, moi nhap lai: ");
	goto nhapcoso;
	}
	if(coso_o==16)
	{
		fflush(stdin);
		printf("Nhap so thu nhat: ");
		gets(strH1);
		printf("Nhap so thu hai: ");
		gets(strH2);
		printf("Nhap he co so ma ban muon chuyen: ");
		scanf("%d", &coso_c);
		x=Hex_to_Dec(strH1);
		y=Hex_to_Dec(strH2);
		printf("Tong, hieu, tich, thuong tuong ung o he co so %d la: ",  coso_c);
		printf("\nTong la: ");dec_to_n(x+y,coso_c);
		if(x>=y)
		{printf("\nHieu la: ");dec_to_n(x-y,coso_c);}
		if(x<y) {if(coso_c==2){printf("\nHieu la: 1");dec_to_n(y-x,coso_c);}
			  else {printf("\nHieu la: -");dec_to_n(y-x,coso_c);}}
		printf("\nTich la: ");dec_to_n(x*y,coso_c);
		if(x>=y){printf("\nThuong la: ");dec_to_n(x/y,coso_c);}
		if(x<y){printf("\nThuong la: 0");}
	}
	if(coso_o==10)
	{
		printf("Nhap 2 so: ");
		scanf("%ld%ld", &n,&m);
		printf("Nhap he co so ma ban muon chuyen: ");
		scanf("%d", &coso_c);
		printf("Tong, hieu, tich, thuong tuong ung o he co so %d la: ", coso_c);
		printf("\nTong la: ");dec_to_n((n+m), coso_c);
		if(n>=m){printf("\nHieu la: ");dec_to_n(n-m,coso_c);}
		if(n<m){if(coso_c==2){printf("\nHieu la: 1");dec_to_n(m-n, coso_c);}
				 else {printf("\nHieu la: -");dec_to_n(m-n,coso_c);}}
		printf("\nTich la: ");dec_to_n(n*m, coso_c);
		if(n>=m){printf("\nThuong la: ");dec_to_n(n/m, coso_c);}
		if(n<m){printf("\nThuong la: 0");}
	}
	if(1<coso_o&&coso_o<10)
	{
		fflush(stdin);
		nhapso:
		{
		printf("Nhap 2 so: ");
		scanf("%ld%ld", &n,&m);
		}
		if(test(n,coso_o)!=0||test(m,coso_o)!=0)
		{
		printf("Nhap sai, moi nhap lai: ");
		goto nhapso;
		}
		else
		{
			printf("Nhap he co so ma ban muon chuyen: ");
			scanf("%d", &coso_c);
			printf("Tong, hieu, tich, thuong tuong ung o he co so %d la: ", coso_c);
			printf("\nTong la: ");dec_to_n(n_to_Dec(n+m,coso_o), coso_c);
			if(n>=m){printf("\nHieu la: ");dec_to_n(n_to_Dec(n-m,coso_o), coso_c);}
			if(n<m){if(coso_c==2){printf("\nHieu la: 1");dec_to_n(n_to_Dec(n-m,coso_o), coso_c);}
					else {printf("\nHieu la: -");dec_to_n(n_to_Dec(m-n,coso_o), coso_c);}}
			printf("\nTich la: ");dec_to_n(n_to_Dec(n*m,coso_o), coso_c);
			if(n>=m){printf("\nThuong la: ");dec_to_n(n_to_Dec(n/m,coso_o), coso_c);}
			if(n<m)printf("\nThuong la: 0");
		}
	}
	getch();
}
